package com.ht.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author d
 * @version 1.0
 * @description: TODO
 * @date 2021/7/8 16:48
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    //入库操作是调用 created/updated
    //setFieldValByName(arg1,arg2,arg3)
    //arg1:自动填充的字段名称 arg2: 自动填充的值  arg3: metaObject 固定写法
    @Override
    public void insertFill(MetaObject metaObject) {
        //设定时间变量
        Date date = new Date();
        this.setFieldValByName("created", date, metaObject);
        this.setFieldValByName("updated", date, metaObject);
    }

    //更新操作是调用 updated
    @Override
    public void updateFill(MetaObject metaObject) {
        Date date = new Date();
        this.setFieldValByName("updated", date, metaObject);
    }
}
